//
//  RinseMoneyController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/10.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "RinseMoneyController.h"
#import <CoreImage/CoreImage.h>
@interface RinseMoneyController ()
@property (weak, nonatomic) IBOutlet UIImageView *addressImg;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;


- (IBAction)preserveAcion:(UIButton *)sender;
- (IBAction)duplicateAction:(UIButton *)sender;

@end

@implementation RinseMoneyController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
    [self.sureButton setBackgroundImage:[UIImage imageWithColor:RGBColor(86, 54, 157)] forState:(UIControlStateHighlighted)];
    
    [self requstData];
    
}
- (void)requstData {
    
    NSDictionary *param = @{
                            @"currency_mark" : self.mark};
    [LSRequestManager getRequestWithURL:HUOBI(@"api/deposit/depositAddress")
                                 params:param
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        NSString*address=[NSString stringWithFormat:@"%@",responseData[@"data"][@"address"]];
                                        self.addressLabel.text=address;
                                        
                                        
                                        CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
                                        [filter setDefaults];
                                        NSData *data  = [address dataUsingEncoding:NSUTF8StringEncoding];
                                        [filter setValue:data forKey:@"inputMessage"];
                                        CIImage *outputImage = [filter outputImage];
                                        self.addressImg.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
                                        
                                        
                                        
                                        
                                    } else {
                                        self.addressLabel.text=@"";
                                        self.addressImg.image=nil;
                                        [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    self.addressLabel.text=@"";
                                    self.addressImg.image=nil;
                                }
                                showHUD:NO];
    
    
    
    
}

- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 0;
}



- (IBAction)preserveAcion:(UIButton *)sender {
    UIImageWriteToSavedPhotosAlbum(self.addressImg.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        [MBProgressHUD showError:@"保存失败"];
    } else {
        [MBProgressHUD showSuccess:@"成功保存到相册"];
    }
}
- (IBAction)duplicateAction:(UIButton *)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.addressLabel.text;
    [MBProgressHUD showSuccess:@"已复制到剪贴板"];
}


- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    
    CGRect extent = CGRectIntegral(image.extent);
    
    //设置比例
    
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 创建bitmap（位图）;
    
    size_t width = CGRectGetWidth(extent) * scale;
    
    size_t height = CGRectGetHeight(extent) * scale;
    
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    
    CGContextScaleCTM(bitmapRef, scale, scale);
    
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 保存bitmap到图片
    
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    
    CGContextRelease(bitmapRef);
    
    
    CGImageRelease(bitmapImage);
    
    return [UIImage imageWithCGImage:scaledImage];
    
}
@end
